package cn.sx.stream.sender.controller;

import cn.sx.stream.common.pojo.MyEvent;
import cn.sx.stream.sender.output.StreamOutputSource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
public class SenderController {

    //将通道数据源source注入 获取通道 在接到send接口请求之后 发送一个消息到总线中
    @Autowired
    private StreamOutputSource source;

    @GetMapping("/send")
    public String sendCoupon(){
        //1.拿到通道
        MessageChannel output01 = source.getStreamOutput01();
        //2.构建一个消息对象 spring框架 构建消息对象,只要是spring封装过的代码 基本上消息对象都是同一个Message
        //封装消息对象的代码 也相同
        MyEvent myEvent = new MyEvent();
        myEvent.setName("张三");
        myEvent.setAge(18);
        myEvent.setCode("001");
        log.info("发送消息");
        Message<MyEvent> message = MessageBuilder.withPayload(myEvent).build();
        output01.send(message);
        return "success";
    }
    @GetMapping("/send02")
    public String sendCoupon02(){
        //1.拿到通道
        MessageChannel output02 = source.getStreamOutput02();
        //2.构建一个消息对象 spring框架 构建消息对象,只要是spring封装过的代码 基本上消息对象都是同一个Message
        //封装消息对象的代码 也相同
        MyEvent myEvent = new MyEvent();
        myEvent.setName("张三");
        myEvent.setAge(18);
        myEvent.setCode("001");
        log.info("发送消息");
        Message<MyEvent> message = MessageBuilder.withPayload(myEvent).build();
        output02.send(message);
        return "success";
    }
}
